<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>hijack_instance_method (Hijacker)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File lib/hijacker.rb, line 23</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">hijack_instance_method</span>(<span class="ruby-identifier">method_name</span>, <span class="ruby-identifier">eval_string</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">arg_names</span> = [], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
    <span class="ruby-identifier">method_name</span> = <span class="ruby-identifier">method_name</span>.<span class="ruby-identifier">to_s</span>
    <span class="ruby-comment cmt"># You have got love ruby!  What other language allows you to pillage and plunder a class like this? </span>
    <span class="ruby-ivar">@store</span> <span class="ruby-operator">&lt;&lt;</span> [
      <span class="ruby-identifier">method_name</span>, 
      <span class="ruby-ivar">@target_klass</span>.<span class="ruby-identifier">instance_methods</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">method_name</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@target_klass</span>.<span class="ruby-identifier">instance_method</span>(<span class="ruby-identifier">method_name</span>)
    ]
    
    <span class="ruby-ivar">@target_klass</span>.<span class="ruby-identifier">send</span> <span class="ruby-identifier">:undef_method</span>, <span class="ruby-identifier">method_name</span>
    <span class="ruby-keyword kw">if</span> <span class="ruby-constant">Symbol</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">eval_string</span>
      <span class="ruby-ivar">@target_klass</span>.<span class="ruby-identifier">send</span> <span class="ruby-identifier">:define_method</span>, <span class="ruby-identifier">method_name</span>, <span class="ruby-ivar">@target_klass</span>.<span class="ruby-identifier">instance_methods</span>(<span class="ruby-identifier">eval_string</span>)
    <span class="ruby-keyword kw">elsif</span> <span class="ruby-constant">String</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">eval_string</span>
      <span class="ruby-ivar">@target_klass</span>.<span class="ruby-identifier">class_eval</span> <span class="ruby-value str">&quot;def \#{method_name}(\#{arg_names * ','})\n\#{eval_string}\nend\n&quot;</span> 
    <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">block_given?</span>
      <span class="ruby-ivar">@target_klass</span>.<span class="ruby-identifier">send</span> <span class="ruby-identifier">:define_method</span>, <span class="ruby-identifier">method_name</span>, <span class="ruby-identifier">block</span>
    <span class="ruby-keyword kw">end</span>
    
    <span class="ruby-keyword kw">true</span>
  <span class="ruby-keyword kw">rescue</span>
    <span class="ruby-keyword kw">false</span>
  <span class="ruby-keyword kw">end</span></pre>
</body>
</html>